Přihlašuji uživatele

C++ - Obousměrný seznam pomocí pointerů

Autor: MarMar
Přidáno: 21.03.2014 13:03
Zobrazení: 721x krát
Tagy: Programování - C++


Zdrojový kód na implementaci obousměrného seznamu pomocí pointerů

Na škole jsem vysledoval, že většina lidí má s tímto problém, tak jsem dám své (snad bez chyby)

 

Soubor seznam.h

#ifndef __SEZNAM_H__
#define __SEZNAM_H__

#include "stdafx.h"

typedef int ItemContent;

class ListItem{
private:
	ListItem* prev;
	ListItem* next;
	ItemContent value;

public:
	ListItem();
	~ListItem();
	
	void SetValue(ItemContent NEWvalue);	//Nastavi hodnotu polozce
	ItemContent GetValue();	//Vrati hodnotu polozky

	void SetNext(ListItem* NEWnext);	//Nastavi nasledujici polozku
	void SetPrev(ListItem* NEWprev);	//Nastavi predchozi polozku
	ListItem* Next();	//Vrati nasledujici polozku
	ListItem* Prev();	//Vrati predchozi polozku
};

class List{
private:
	ListItem* head;
	ListItem* current;

	ListItem* Head();	//Vrati hlavicku
	ListItem* Current();	//Vrati iterator
	void SetHead(ListItem* NEWhead);	//Nastavi hlavicku
	void SetCurrent(ListItem* NEWcurrent);	//Nastavi iterator

	ListItem* GetListItem(int index);	//Vrati polozku z index

	void Reset();	//Resetovat iterator
	ListItem* Last();	//Vrati posledni polozku

	bool ValidIndex(int index, bool bounds=false);	//Kontrola platneho indexu
	
	void MoveNext();	//Posunout iterator na dalsi pozici


public:
	List();
	~List();

	void Insert(ItemContent value);	//Prida polozku na konec seznamu
	bool Insert(int index, ItemContent value);	//Prida polozku do seznamu na urcitou pozici

	bool Delete();	//Smaze posledni polozku
	bool Delete(int index);	//Smaze polozku z urcite pozice

	bool Get(int index, ItemContent &Return);	//Vrati hodnotu polozky z index
	bool Set(int index, ItemContent value);	//Nastavi polozku s indexe na hodnotu value

	bool Exchange(int index1, int index2);	//Zamenit polozky
	
	bool Move(int act_index, int new_index);	//Presunout polozku

	int Search(ItemContent value);	//Vyhledani polozky + navraceni indexu (-1 -> nenalezeno)

	int Count();	//Vrati pocet zaznamu v seznamu
	bool IsEmpty();	//Vraci bool hodnotu, zda je seznam prazdny
};

#endif;

Soubor seznam.cpp

#include "stdafx.h"
#include "seznam.h"

#include 
#include 
#include 




ListItem::ListItem(){
	this->next = NULL;
	this->prev = NULL;
	this->value = 0;
}

ListItem::~ListItem(){

}

void ListItem::SetValue(ItemContent NEWvalue){
	this->value = NEWvalue;
}

void ListItem::SetNext(ListItem* NEWnext){
	this->next = NEWnext;
}

void ListItem::SetPrev(ListItem* NEWprev){
	this->prev = NEWprev;
}

ItemContent ListItem::GetValue(){
	return this->value;
}

ListItem* ListItem::Next(){
	return this->next;
}

ListItem* ListItem::Prev(){
	return this->prev;
}












List::List(){
	this->head = NULL;
	this->current = NULL;
}

List::~List(){
	for (int i = 0; i < this->Count(); i++){
		this->Delete();
	}
}

ListItem* List::Head(){
	return this->head;
}

ListItem* List::Current(){
	return this->current;
}

void List::Insert(ItemContent value){
	ListItem *nLi = new ListItem();
	nLi->SetValue(value);

	if (this->IsEmpty() == true){	//Vložit na první prvek -> místo hlavy
		nLi->SetPrev(NULL);
		
		this->head = nLi;
		this->current = nLi;
	}
	else{	//Vložit na konec seznamu
		ListItem *last = this->Last();
		
		last->SetNext(nLi);
		nLi->SetPrev(last);			
	}

	nLi->SetNext(NULL);
}

bool List::Insert(int index, ItemContent value){
	if (ValidIndex(index, true)){
		if (index == this->Count()){	//Vlozit na konec
			this->Insert(value);
		}
		else{
			ListItem *nLi = new ListItem();
			nLi->SetValue(value);	

			ListItem *actindex = this->GetListItem(index);

			nLi->SetPrev(actindex->Prev());
			nLi->SetNext(actindex);

			if (actindex->Prev() != NULL){ actindex->Prev()->SetNext(nLi); }
			actindex->SetPrev(nLi);		

			//Nova hlava
			if (index == 0){ this->head = nLi; }
		}

		return true;
	}

	return false;
}

bool List::Delete(){	
	ListItem *last = this->Last();
	
	if (last != NULL){
		if (last->Prev() != NULL){
			last->Prev()->SetNext(NULL);

			delete last;

			return true;
		}
	}

	return false;	
}

bool List::Delete(int index){
	if (ValidIndex(index)){
		ListItem *delitem = this->GetListItem(index);

		ListItem *pred = delitem->Prev();
		ListItem *potom = delitem->Next();

		if (pred != NULL){
			pred->SetNext(potom);
		}

		if (potom != NULL){
			potom->SetPrev(pred);
		}

		if (index == 0){
			this->head = potom;
			this->head->SetPrev(NULL);
		}

		delete delitem;

		return true;
	}

	return false;
}

bool List::Get(int index, ItemContent &Return) {
	int cnt = 0;
	if (ValidIndex(index)){
		ListItem *li = this->GetListItem(index);

		Return = li->GetValue();
		
		return true;
	}
	return false;
}

bool List::Set(int index, ItemContent value){
	int cnt = 0;
	if (ValidIndex(index)){
		ListItem *li = this->GetListItem(index);

		li->SetValue(value);

		return true;
	}
	return false;
}

ListItem* List::GetListItem(int index){
	int cnt = 0;
	if (ValidIndex(index)){
		Reset();

		while (cnt < index){
			this->MoveNext();

			cnt++;
		}

		return this->current;
	}

	return NULL;
}

void List::Reset(){
	this->current = this->head;
}

int List::Count(){
	int cnt = 0;

	this->Reset();
	while (this->current != NULL){
		cnt++;

		this->MoveNext();
	}

	return cnt;
}

bool List::IsEmpty(){
	if (this->head == NULL){ return true; }

	return false;
}

ListItem* List::Last(){
	this->Reset();
	while (this->current->Next() != NULL){
		this->MoveNext();
	}

	return this->current;
}

bool List::ValidIndex(int index, bool bounds){
	if (bounds==false){
		if ((index >= 0) && (index < this->Count())){ return true; }
	}
	else{
		if ((index >= 0) && (index <= this->Count())){ return true; }
	}
	
	return false;
}

bool List::Exchange(int index1, int index2){
	if (this->ValidIndex(index1)){
		if (this->ValidIndex(index2)){
			ListItem *pol1 = this->GetListItem(index1);
			ListItem *pol2 = this->GetListItem(index2);

			ItemContent old = pol1->GetValue();
			pol1->SetValue(pol2->GetValue());
			pol2->SetValue(old);

			return true;
		}
	}

	return false;
}

bool List::Move(int act_index, int new_index){
	if (this->ValidIndex(act_index)){
		if (this->ValidIndex(new_index)){
			if (act_index != new_index){
				ListItem *pol1 = this->GetListItem(act_index);
				ListItem *pol2 = this->GetListItem(new_index);

				ItemContent val = pol1->GetValue();
				ItemContent val2 = pol2->GetValue();

				if (act_index == 0){	//Presunuti hlavy
					ListItem *nH = pol1->Next();
					this->Delete(act_index);

					this->head = nH;
					this->head->SetPrev(NULL);	

					this->Insert(new_index, val);

					return true;
				}

				if (new_index == 0){	//Nova hlava
					this->Delete(act_index);
					this->Insert(new_index, val);

					return true;
				}


				this->Delete(act_index);
				if (act_index < new_index){					
					this->Insert(new_index - act_index + 1, val);
				}
				else{
					this->Insert(new_index, val);
				}
			

				return true;
			}
		}
	}

	return false;
}

int List::Search(ItemContent value){
	for (int i = 0; i < this->Count(); i++){
		ItemContent val;
		this->Get(i, val);

		if (val == value){
			return i;
		}
	}

	return -1;
}

void List::MoveNext(){
	this->current = this->current->Next();
}


Komentáře:

Od: Anonymní uživatel
Název:

Zpráva:
Kód:
Řazení: Od nejnovějšího
Anonymní uživatel - STEKLOIZDELIE - 2024-04-22 15:13:21
Anonymní uživatel - Bonnie Goris - 2024-03-31 13:05:14
This Small Investment Of $133 Banked Us $634 With No Work Required More: https://www.youtube.com/watch?v=VRI64YKb-dY?wz.cz
Anonymní uživatel - Ukraine - 2023-02-05 10:47:46
Ukraine